﻿using System;
using System.Runtime.Serialization;
using System.Xml.Linq;

namespace XamlaApp.ViewModels
{

    /// <summary>
    /// Provides View Model Properties for all Xamla Element Views and Property Views
    /// </summary>
    [DataContract]
    public class BaseElementViewModel : BaseViewModel
    {
        public BaseElementViewModel()
        {
            MinElementWidth = MinElementHeight = 10;
            MaxElementWidth = MaxElementHeight = 1000;

        }
        public double MinElementWidth { get; private set; }
        public double MaxElementWidth { get; private set; }
        public double MinElementHeight { get; private set; }
        public double MaxElementHeight { get; private set; }

        //supporting views that have a secondary edit mode, like textblock
        private bool _editMode;
        public bool EditMode
        {
            get { return _editMode; }
            set
            {
                _editMode = value;
                PropertyChanging("EditMode");
            }
        }

        private bool _selected;
        public bool IsActiveElement
        {
            get { return _selected; }
            set
            {
                _selected = value;
                PropertyChanging("IsActiveElement");
            }
        }

        private string _controlName = "[undefined]";
        [DataMember]
        public string ControlName
        {
            set
            {
                _controlName = value;
                PropertyChanging("ControlName");
                var mvm = Container.AppContainer.Resolve<MainUIViewModel>();
                mvm.StageViewElementsChanged(this, new EventArgs());
            }
            get { return _controlName; }
        }

        internal override System.Xml.Linq.XElement GenerateXaml(XElement element)
        {
            //only add the control name if it is not default
            if (!ControlName.StartsWith("[") && !ControlName.EndsWith("]"))
            {
                var xns = XNamespace.Get("http://schemas.microsoft.com/winfx/2006/xaml");
                element.Add(new XAttribute(xns + "Name", ControlName));
            }
            return base.GenerateXaml(element);
        }
    }
}
